package me.keiwu.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.keiwu.admin.domain.config.Config;
import me.keiwu.admin.service.ConfigService;
import me.keiwu.pub.data.Response;
import me.keiwu.pub.data.ConfigType;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.config.AppConfigBean;
import me.keiwu.pub.data.config.ChallengeConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by kei on 05/12/2017.
 */
@RestController
@RequestMapping("/api/config")
public class ConfigController {


    @Autowired
    private ConfigService configService;


    @RequestMapping("/get")
    public ResponseV2 getConfig(Integer configType) {
        ConfigType type = ConfigType.parse(configType);
        Config config = configService.getConfig(type);
        return ResponseV2.ok(config);
    }

    @RequestMapping("/getAll")
    public ResponseV2 getAllConfig() {
        return ResponseV2.ok(configService.getAllConfig());
    }

    @RequestMapping("/set")
    public ResponseV2 getConfig(Integer configType, String configJson) {
        ConfigType type = ConfigType.parse(configType);
        if (type == null) return ResponseV2.fail("不存在该配置id");
        JSONObject json = JSON.parseObject(configJson);
        if (json == null) return ResponseV2.fail("配置格式正确");
        json.put("configType", type.getType());

        Config config = new Config();
        config.setConfigType(type.getType());
        config.setConfigJson(json.toJSONString());
        configService.setConfig(config);
        return ResponseV2.ok(config);
    }



    @RequestMapping("/gold/set")
    public Response setGoldConfig(Integer awardGold,
                                  Integer challengeCostGold,
                                  @RequestParam(defaultValue = "999") Integer maxTask) {
        // 挑战配置
        Config challengeConfig = new Config();
        challengeConfig.setConfigType(ConfigType.CHALLENGE_CONFIG.getType());
        ChallengeConfigBean challengeBean = new ChallengeConfigBean();
        challengeBean.setConfigType(ConfigType.CHALLENGE_CONFIG.getType());
        challengeBean.setAwardGold(awardGold);
        challengeBean.setChallengeCostGold(challengeCostGold);
        challengeBean.setMaxTask(maxTask);

        challengeConfig.setConfigJson(JSON.toJSONString(challengeBean));
        configService.setConfig(challengeConfig);

        JSONObject json = new JSONObject();
        json.put("challengeConfig", challengeConfig);
        json.put("stepConfig", challengeConfig);
        return Response.ok(json);
    }


    @RequestMapping("/gold/get")
    public Response getGoldConfig() {
        Config challengeConfig = configService.getConfig(ConfigType.CHALLENGE_CONFIG);

        JSONObject json = new JSONObject();
        json.put("challengeConfig", challengeConfig);
        json.put("stepConfig", challengeConfig);
        return Response.ok(json);
    }



    @RequestMapping("/app/set")
    public Response setAppConfig(String forwardDesc,
                                 String forwardCover,
                                 String groupQRCode,
                                 @RequestParam(defaultValue = "100") Integer initGold,
                                 @RequestParam(defaultValue = "15000") Integer waterBallMax) {

        AppConfigBean appConfig = new AppConfigBean();
        appConfig.setConfigType(ConfigType.APP_CONFIG.getType());
        appConfig.setForwardDesc(forwardDesc);
        appConfig.setForwardCover(forwardCover);
        appConfig.setGroupQRCode(groupQRCode);
        appConfig.setInitGold(initGold);
        appConfig.setWaterBallMax(waterBallMax);

        Config config = new Config();
        config.setConfigType(ConfigType.APP_CONFIG.getType());
        config.setConfigJson(JSON.toJSONString(appConfig));
        configService.setConfig(config);

        return Response.ok(config);
    }


    @RequestMapping("/app/get")
    public Response getAppConfig() {
        Config config = configService.getConfig(ConfigType.APP_CONFIG);
        return Response.ok(config);
    }

}
